package szwolaiyun.paireach.com.szwolaizhuang.activity;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Spinner;
import android.widget.TextView;

import com.paireach.szwolaiyun.sz_common_widget.activity.BaseActivity;
import com.paireach.szwolaiyun.sz_common_widget.adapter.AppBaseAdapter;
import com.paireach.szwolaiyun.sz_common_widget.adapter.ViewHolder;
import com.paireach.szwolaiyun.sz_util.SharedPreferencesUtil;

import org.json.JSONObject;

import java.util.List;

import szwolaiyun.paireach.com.szwolaizhuang.R;
import szwolaiyun.paireach.com.szwolaizhuang.model.business.Operator;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.AppEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.HttpErrorEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.HttpSuccessEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.requestTag.RequestTag;
import szwolaiyun.paireach.com.szwolaizhuang.net.UserRequest;
import szwolaiyun.paireach.com.szwolaizhuang.net.WayBillRequest;
import szwolaiyun.paireach.com.szwolaizhuang.net.json.JsonPaser;

/**
 * 选择叉车工界面
 * 业务逻辑：获取叉车工列表，点击按钮后首先先装货卸货操作，此操作不会失败；然后提交叉车工，不用管提交成功还是失败即可
 */
public class SelectFriendActivity extends BaseActivity implements View.OnClickListener {

    private Button btn1, btn2;
    private Spinner spinner;
    private CheckBox checkBox;
    private TextView text1;
    private Adapter adapter;

    private String workType;//装车 还是 卸车
    private String orderNo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_friend);
        orderNo = getIntent().getStringExtra("no");
        workType = getIntent().getStringExtra("workType");
        adapter = new Adapter(this);
        initView(orderNo);
        获取叉车工列表();
    }

    private void 获取叉车工列表() {
        showProgressDialog();
        String ID = SharedPreferencesUtil.getValue(this, SharedPreferencesUtil.ID);
        new UserRequest().获取叉车工列表(ID);
    }

    private void 装货卸货() {
        showProgressDialog();
        new WayBillRequest().叉车工装卸货操作(orderNo);
    }

    private void 保存叉车工() {
        showProgressDialog();
        String id = SharedPreferencesUtil.getValue(this, SharedPreferencesUtil.ID);
        String paId = ((Operator) spinner.getSelectedItem()).getId() + "";
        new UserRequest().保存叉车工(workType, id, paId, orderNo);
    }

    private void initView(String orderNo) {
        ((TextView) findViewById(R.id.layout_top_change)).setText("选择叉车工");
        findViewById(R.id.layout_top_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        checkBox = (CheckBox) findViewById(R.id.checkbox);
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        spinner = (Spinner) findViewById(R.id.spinner);
        checkBox.setChecked(true);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    spinner.setEnabled(true);
                } else {
                    spinner.setEnabled(false);
                }
            }
        });
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        text1 = (TextView) findViewById(R.id.text1);
        text1.setText(orderNo);
        spinner.setAdapter(adapter);

    }

    @Override
    public void onClick(View v) {
        if (v == btn1) {
            装货卸货();
        } else if (v == btn2) {
            finish();
        }
    }

    @Override
    public void applicationEvent(AppEvent event) {
        super.applicationEvent(event);
        if (event.getTag().equals("获取叉车工列表")) {
            if (event.getCode() == 0) {
                List<Operator> list = (List<Operator>) event.getObj1();
                adapter.setListAndRefresh(list);
                //addition op
                if (list == null || list.size() == 0) {
                    checkBox.setChecked(false);
                    checkBox.setEnabled(false);
                }
            } else {
                showToast(event.getExtraInfo());
                checkBox.setChecked(false);
                checkBox.setEnabled(false);
            }
        }
    }

    @Override
    public void httpSuccessEvent(HttpSuccessEvent event) {
        super.httpSuccessEvent(event);
        if (event.getRequestTag() == RequestTag.获取叉车工列表) {
            Log.d("SelectFriendActivity", "网络调用：获取所有叉车工列表成功");
            dismissProgressDialog();
            JsonPaser.获取叉车工列表(event.getJson());
        } else if (event.getRequestTag() == RequestTag.保存叉车工) {
            Log.d("SelectFriendActivity", "网络调用：保存选定的叉车工成功");
            dismissProgressDialog();
            finish();
        } else if (event.getRequestTag() == RequestTag.叉车工装卸货操作) {
            Log.d("SelectFriendActivity", "网络调用：装货/卸货成功");
            dismissProgressDialog();
            try {
                JSONObject object = new JSONObject(event.getJson());
                if (object.getString("loadType").equals("0")) {
                    showToast(object.getString("content"));
                    findViewById(R.id.layout1).setVisibility(View.GONE);
                    TextView tv = (TextView) findViewById(R.id.layout2);
                    tv.setVisibility(View.VISIBLE);
                    tv.setText(object.getString("content"));
                } else {
                    if (checkBox.isChecked()) {
                        if (adapter.getCount() == 0) {
                            showToast("扫码完成");
                            finish();
                        } else
                            保存叉车工();
                    } else {
                        showToast("扫码完成");
                        finish();
                    }
                }
            } catch (Exception e) {
                showToast("解析数据出错");
            }
        }
    }

    @Override
    public void httpErrorEvent(HttpErrorEvent event) {
        super.httpErrorEvent(event);
        if (event.getRequestTag() == RequestTag.获取叉车工列表) {
            dismissProgressDialog();
            showToast(event.getErrorMessage());
        } else if (event.getRequestTag() == RequestTag.保存叉车工) {
            dismissProgressDialog();
            showToast(event.getErrorMessage());
        } else if (event.getRequestTag() == RequestTag.叉车工装卸货操作) {
            dismissProgressDialog();
            showToast(event.getErrorMessage());
        }
    }

    private class Adapter extends AppBaseAdapter {

        public Adapter(Context mContext) {
            super(mContext);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = ViewHolder.get(getContext(), convertView, parent, android.R.layout.simple_list_item_1, position);
            Operator operator = (Operator) getItem(position);
            ((TextView) holder.getView(android.R.id.text1)).setText(operator.getRealName());
            return holder.getConvertView();
        }
    }
}
